package org.example.spring.self.demo.tl.c02.applicationlistener;

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.event.SimpleApplicationEventMulticaster;

import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@ComponentScan
@Configuration
public class MainConfig {

	@Bean
	public ThreadPoolExecutor defaultThreadPoolExecutor() {
		return new ThreadPoolExecutor(
				2, 5,
				5, TimeUnit.SECONDS,
				new ArrayBlockingQueue<>(1), new ThreadPoolExecutor.CallerRunsPolicy());
	}

	/**
	 * 异步处理
	 *
	 * @param defaultThreadPoolExecutor
	 * @return
	 */
	@Bean
	public SimpleApplicationEventMulticaster applicationEventMulticaster(@Qualifier("defaultThreadPoolExecutor") ThreadPoolExecutor defaultThreadPoolExecutor) {
		SimpleApplicationEventMulticaster simpleApplicationEventMulticaster = new SimpleApplicationEventMulticaster();
		simpleApplicationEventMulticaster.setTaskExecutor(defaultThreadPoolExecutor);
		return simpleApplicationEventMulticaster;
	}

}
